import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {FetchService} from '../../../core/net/fetch.service';
import {NzMessageService, NzTreeNode} from 'ng-zorro-antd';
import {Subject} from "rxjs";
import {takeUntil} from "rxjs/operators";
import {UtilService} from "../../../core/util/util.service";


@Component({
  selector: 'app-project-by-department',
  templateUrl: './project-by-department.component.html',
  styleUrls: ['./project-by-department.component.scss']
})
export class ProjectByDepartmentComponent implements OnInit {
  rightTableData;
  searchValue = "";
  activedNode;
  searchForm: FormGroup;
  treeData;
  unsubscribe: Subject<void> = new Subject<void>();
  defaultKeys: any;
  name = '';

  pageNum = 1;
  pageSize = 10;
  total: number;
  loading = false;
  checked = false;

  constructor(private fb: FormBuilder,
              private utilService: UtilService,
              private fetchService: FetchService,
              private message: NzMessageService) {
    this.searchForm = this.fb.group({
      key: ['']
    });
  }

  ngOnInit(): void {
    this.fetchService.post<any>("witis/aDjjdjd/list", {pageSize: 500})
      .pipe(takeUntil(this.unsubscribe))
      .subscribe(res => {
        this.treeData = this.formatToCascadeData(res, '640000000000000');
        this.getTreeData(this.treeData);
        this.defaultKeys = [this.treeData[0].key];
        this.checkDetail(this.treeData[0]);
      });
  }

  private formatToCascadeData(data, rootId): any {
    const formatArr = [];
    for (const item of data) {
      const obj = {
        id: item.localitycode,
        name: item.localityname,
        pid: item.parentloccode,
        children: []
      };
      formatArr.push(obj);
    }
    return this.listToTreeWithLevel(formatArr, '000000000000000', 0, '', rootId);
  }

  private listToTreeWithLevel(list, parent, level, namePath, rootId?): Array<any> {
    const out = [];
    for (const node of list) {
      if (node.pid == parent) {
        node.level = level;
        if (node.id === rootId) {
          node.namePath = node.name;
        } else {
          node.namePath = namePath + '-' + node.name;
        }
        const children = this.listToTreeWithLevel(list, node.id, level + 1, node.namePath, rootId);
        if (children.length) {
          node.children = children;
        }
        out.push(node);
      }
    }
    return out;
  }

  private getTreeData(data) {
    data.map(item => {
      item.title = item.name;
      item.key = item.id;
      if (item.children && item.children.length) {
        this.getTreeData(item.children);
      } else {
        item.isLeaf = true;
      }
    });
  }

  getGridData(reset: boolean = false): void {
    if (reset) {
      this.pageNum = 1;
    }
    this.loading = true;
    const param = {
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      szxzqdm: this.activedNode.key,
      key: this.searchForm.value.key,
      bjxm: this.checked ? 1 : 0
    };
    this.fetchService.post<any>('witis/aQskhc/listGlbm', param).subscribe(res => {
      this.total = res['total'];
      this.rightTableData = res['list'];
      this.loading = false;
    });
  }

  // 查看
  checkDetail(e) {
    this.activedNode = e.node || e;
    const namePath = this.activedNode.namePath ? this.activedNode.namePath : this.activedNode.origin.namePath;
    this.name = namePath.split('-').join('');
    console.log(this.name);
    this.getGridData();
  }

  onView(data) {
    console.log(data);
  }
}
